package com.bjtct.plugin.autogen.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtils {

	public static Connection getCon(String user, String pwd, String url) throws SQLException {

		return DriverManager.getConnection(url, user, pwd);
	}

	public static void colse(Connection con, ResultSet rs, Statement st) {
		try {
			if (st != null)
				st.close();
			if (rs != null)
				rs.close();
			if (con != null)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static boolean isEmpty(String value) {
		return (null == value || value.length() == 0);
	}

	public static ColumnWrap wrap(DbColumn col) {
		ColumnWrap wrap = new ColumnWrap();
		wrap.setColumn_alias(col.getColumn_name().toLowerCase());
		wrap.setColumn_comment(col.getColumn_comment());
		wrap.setColumn_default(col.getColumn_default());
		wrap.setColumn_key(col.getColumn_key());
		wrap.setColumn_name(col.getColumn_name());
		wrap.setColumn_type(col.getColumn_type());
		wrap.setData_type(getJavatype(col.getData_type()));
		wrap.setIs_nullable(getPage_type(col.getData_type()));

		wrap.setExtra(col.getExtra());
		if (col.getClass().equals("auto_increment")) {
			wrap.setNew_show("N");
		} else {
			wrap.setNew_show("Y");
		}
		if (col.getColumn_key().equals("PRI")) {
			wrap.setList_query("N");
			wrap.setList_show("Y");
			wrap.setUpdate_show("N");
		} else {
			wrap.setList_query("Y");
			wrap.setList_show("Y");
			wrap.setUpdate_show("Y");
		}

		return wrap;

	}

	public static String getPage_type(String dataType) {
		// "textbox", "combobox","datebox","datetimebox"
		String page_type = "textbox";
		switch (dataType.toUpperCase()) {
		case "CHAR":
		case "VARCHAR2":
		case "VARCHAR":
		case "INTEGER":
		case "TINYINT":
		case "SMALLINT":
		case "MEDIUMINT":
		case "NUMBRIC":
		case "NUMBER":
		case "BIT":
		case "FLOAT":
		case "DOUBLE":
		case "DECIMAL": {
			page_type = "textbox";
			break;
		}
		case "ENUM":
		case "SET": {
			page_type = "combobox";break;
		} 
		case "DATE": {
			page_type = "datebox";break;
		}
		case "DATETIME":
		case "TIMESTAMP": {
			page_type = "datetimebox";break;
		}
		}
		return page_type;
	}

	public static String getJavatype(String dataType) {
		String javatype = "int";
		switch (dataType.toUpperCase()) {
		case "CHAR":
			;
		case "VARCHAR2":
			;
		case "VARCHAR":
			;
		case "ENUM":
			;
		case "SET": {
			javatype = "String";
			break;
		}
		case "INTEGER":
			;
		case "TINYINT":
			;
		case "SMALLINT":
			;
		case "MEDIUMINT": {
			javatype = "int";
			break;
		}
		case "NUMBRIC":
			;
		case "NUMBER": {
			javatype = "double";
			break;
		}
		case "BIT": {
			javatype = "boolean";
			break;
		}
		case "FLOAT": {
			javatype = "float";
			break;
		}
		case "DOUBLE": {
			javatype = "double";
			break;
		}
		case "DECIMAL": {
			javatype = "double";
			break;
		}
		case "DATE":{
			javatype = "date";
			break;
		}
		case "DATETIME":
			;
		case "TIMESTAMP": {
			javatype = "datetime";
			break;
		}
		default: {
			javatype = "int";
		}
		}
		return javatype;
	}
}
